<?php

namespace App\Http\Controllers\admin\Goods;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class GoodsCateController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $cates=self::CateList();
            // dd($cates);
            foreach ($cates as $k => $v) {
            if($v->cid != 0){
                $res = DB::table('goodscate')->where('id',$v->cid)->first();
                
                $cates[$k]->path= $res->name;
            }else{
                $cates[$k]->path='顶级分类';
            }
        }
        
        return view('admin.goods.goodsindex',compact('cates'));
    }

     //按照格式查询 分类列表
   static public function CateList()
    {
        $cates = DB::select('select * ,concat(path,",",id) as paths from goodscate order by paths');
        foreach($cates as $k=>$v){
            //把字符串变成数组
            $arr = explode(',',$v->path);
            //获取长度
            $len = count($arr)-1;
            $cates[$k]->name=str_repeat('|-------',$len).$v->name;
        }

        return $cates;
    }




    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $cates=self::CateList();
        return view('admin.goods.goodsadd',compact('cates'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $data = $request->only(['name','cid','id']);

        //拼接路径
        //判断cid是否为0
        if($data['cid']==0){
            $data['path']=0;
        }else{
            //根据cid查询path
            $res = DB::table('goodscate')->where('id',$data['cid'])->first();
            $data['path']=$res->path.','.$data['cid'];
            $data['cid']=$res->id;
        }
        $data['status'] = 1;
        //执行插入数据
        $row = DB::table('goodscate')->insert($data);
        if($row){
            return redirect('/admin/goodscate')->with('success','添加成功');
        }else{
            return back()->with('error','添加成功');

        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        // dd($id);
        //根据用户id查询数据
        $cate = DB::table('goodscate')->where('id',$id)->first();
        $cates = self::CateList();
        //解析模板显示用户信息
        // dd($cate->status);
        return view('admin.goods.goodsedit',compact('cate','cates'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $id = $request->input('id');
       
         //判断是否修改了分类
        if($request->input('id') != $request->input('xpid')){
            //查询该栏目下是否有子栏目
            $row = DB::table('goodscate')->where('cid',$id)->count();
            //要修改分类，查询是否子分类
            if($row > 0){
                return back()->with('error','有子分类不能修改');
            }
        }

        //可以修改
        $data = $request->only(['name','cid','path','status']);
        
        if($data['cid'] == 0){
            $data['path']=0;
        }else{
            //根据cid查询path
            $res = DB::table('goodscate')->where('id',$data['cid'])->first();
            $data['path']=$res->path.','.$data['cid'];
        }

         //执行数据插入
        $res = DB::table('goodscate')->where('id',$id)->update($data);
        if($res){
            return redirect('/admin/goodscate')->with('success','分类修改成功');
        }else{
            return back()->with('error','分类修改失败');
        }
        
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        // dd($id);
        $row = DB::table('goodscate')->where('id',$id)->delete();
        if($row > 0){
            return redirect('admin/goodscate')->with('success','删除成功');
        }
    }
}
